package rursu.dal.loaders;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import rursu.business.ProgressListener;
import rursu.business.pojo.Movie;
import rursu.business.pojo.MovieDetails;
import rursu.business.pojo.MovieImage;
import rursu.dal.managers.DataBaseManager;
import rursu.dal.managers.interfaces.IMovieDAL;
import rursu.dal.managers.interfaces.IMovieDetailsDAL;
import rursu.dal.managers.interfaces.IMovieImageDAL;
import rursu.parser.MovieDetailsParser;
import rursu.parser.MovieDetailsSAXParser;
import rursu.providers.callback.MovieDetailsCallback;
import android.os.AsyncTask;

public class MovieDetailsLoaderTask extends AsyncTask<Integer, Void, MovieDetails> {

	private ProgressListener progressListener;
	private MovieDetailsCallback detailsCallback;
	private IMovieDetailsDAL movieDetailsDALManager;
	private IMovieDAL movieDALManager;
	private IMovieImageDAL movieImageDALManager;
	private DataBaseManager dataBaseManager;
	private String detailsURL;
	
	public MovieDetailsLoaderTask(MovieDetailsCallback detailsCallback) {
		super();
		this.detailsCallback = detailsCallback;
	}

	@Override
	protected void onPreExecute() {
		if ( progressListener != null ) {
			progressListener.progressContinue();
		}
		detailsCallback.onDataPreDownload();
	}
	
	@Override
	protected MovieDetails doInBackground(Integer... params) {
		
		movieDetailsDALManager = dataBaseManager.getMovieDetailsDALManager();
		movieDALManager = dataBaseManager.getMovieDALManager();
		movieImageDALManager = dataBaseManager.getMovieImageDALManager();
		
		MovieDetailsParser movieDetailsParser = new MovieDetailsSAXParser();
		
		MovieDetails details = movieDetailsDALManager.get(params[0]);
		
		if (details == null) {
			
			InputStream inputStream = null;
			URL url = null;
			
			try {
				url = new URL(getDetailsURL() + params[0]);
				inputStream = url.openStream();
				
			} catch (MalformedURLException e) {			
				
				e.printStackTrace();
				
			} catch (IOException e) {
				
				e.printStackTrace();
				
			} 
			
			details = movieDetailsParser.parse(inputStream);
			
			movieDetailsDALManager.insert(details);
			
			// images loading
			for ( MovieImage image : details.getImages() ) {
				movieImageDALManager.insert(image, details.getId(), null);
			}
		}
		
		Movie movie = movieDALManager.get(params[0]);
		
		details.setRating(movie.getRating());
		details.setOverview(movie.getOverview());
		details.setName(movie.getName());
		details.setReleased(movie.getReleased());
		
		return details;
	}
	
	@Override
	protected void onPostExecute(MovieDetails result) {
		super.onPostExecute(result);
		
		if ( progressListener != null ) {
			progressListener.progressFinished();
		}
	
		detailsCallback.onDataDownloaded(result);
	}
	
	@Override
	protected void onCancelled() {
		super.onCancelled();
		
		if ( progressListener != null ) {
			progressListener.progressFinished();
		}	
	}
	
	public ProgressListener getProgressListener() {
		return progressListener;
	}

	public void setProgressListener(ProgressListener progressListener) {
		this.progressListener = progressListener;
	}

	public MovieDetailsCallback getDetailsCallback() {
		return detailsCallback;
	}

	public void setDetailsCallback(MovieDetailsCallback detailsCallback) {
		this.detailsCallback = detailsCallback;
	}

	public DataBaseManager getDataBaseManager() {
		return dataBaseManager;
	}

	public void setDataBaseManager(DataBaseManager dataBaseManager) {
		this.dataBaseManager = dataBaseManager;
	}

	public String getDetailsURL() {
		return detailsURL;
	}

	public void setDetailsURL(String detailsURL) {
		this.detailsURL = detailsURL;
	}
	
	
	
}
